package com.example.demo.error;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.example.demo.error.costom.PasswordException;
import com.example.demo.error.costom.PhoneNumberAlreadyExistsException;
import com.example.demo.result.Result;
import org.quartz.ObjectAlreadyExistsException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.stream.Collectors;

/*
 *
 *   @Author: Sjy
 *   @Date: 2024/3/6-03-06-19:58
 *   参数全局异常拦截类
 */
@RestController
@ControllerAdvice
public class ParameterError {

    //捕获空指针异常
    /*@ExceptionHandler(NullPointerException.class)
    public String nullPointerException(){
        return "空指针异常";
    }*/

    //参数校验异常拦截
    @ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    public ResponseEntity<Result<String>> handleValidatedException(Exception e) {
        Result<String>  result = null;
        if (e instanceof  MethodArgumentNotValidException) {
            // 强制类型转换
            MethodArgumentNotValidException ex =(MethodArgumentNotValidException)  e;
            result = Result.error(86202,"参数校验异常",
                    ex.getBindingResult().getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.toList())
            );
        } else  if (e instanceof ConstraintViolationException){
            ConstraintViolationException ex = (ConstraintViolationException) e;
            result = Result.error(86202,"参数校验异常",
                    ex.getConstraintViolations().stream()
                            .map(ConstraintViolation::getMessage)
                            .collect(Collectors.toList())
            );
        }else  if (e instanceof BindException) {
            BindException ex = (BindException ) e;
            result = Result.error(86202,"参数校验异常",
                    ex.getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.toList())
            );
        }
        return new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
    }


    @ExceptionHandler(PhoneNumberAlreadyExistsException.class)
    public Result phoneNumberAlreadyExistsException(){
        return Result.error().code(86204).msg("手机号已被注册");
    }

    @ExceptionHandler(PasswordException.class)
    public Result passwordException(){
        return Result.error().code(86203).msg("用户名或密码错误");
    }

    //TODO 后面来吧这个换成401状态码
    @ExceptionHandler(NotLoginException.class)
    public Result notLoginException(){return Result.error().code(86205).msg("未能读取到有效token");}


    @ExceptionHandler(NotRoleException.class)
    public Result notRoleException(NotRoleException notRoleException){
        return Result.error().code(86206).msg(notRoleException.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public Result exception(NotPermissionException notPermissionException){
        return Result.error().code(86207).msg(notPermissionException.getMessage());
    }

    @ExceptionHandler(ObjectAlreadyExistsException.class)
    public Result exception(ObjectAlreadyExistsException objectAlreadyExistsException){
        return Result.error().code(86208).msg("Job名字重复");
    }


}
